package org.niux.aims.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.service.IAccountService;
import org.niux.aims.service.IEntitysService;
import org.niux.aims.service.ILogsService;
import org.niux.aims.vo.AccountVO;
import org.niux.aims.vo.AuthVO;
import org.niux.aims.vo.EntityVO;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：AssignmentAuthsAction.java<br>
 * 摘    要：权限分配页面类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-9-4<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class AssignmentAuthsAction extends AimsBaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1965512802061388366L;

	@Resource(name="entityService")
	private IEntitysService entityService;
	
	@Resource(name="accountService")
	private IAccountService accountService;
	
	@Resource(name="logsService")
	private ILogsService logsService;
	
	private String accountId;
	private List<EntityVO> entitys;
	private List<String> entityIds;
	private AccountVO account;
	
	/**
	 * 
	 * 方法概述：页面渲染
	 * 描述：页面渲染
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-9-4
	 */
	public String showPage(){
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】进入权限分配界面");
		account = accountService.findById(Integer.parseInt(accountId));
		entitys = entityService.findByApp(account.getApp().getId());
		return SUCCESS;
	}
	
	/**
	 * 
	 * 方法概述：分配权限
	 * 描述：分配权限
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-9-5
	 */
	public String assignmentAuths(){
		List<AuthVO> auths = new ArrayList<AuthVO>();
		
		AccountVO account = accountService.findById(Integer.parseInt(accountId));
		
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】分配【"+account.getName()+"】的权限");
		
		for (String id : entityIds) {
			EntityVO entity = new EntityVO();
			entity.setId(id);
			
			AuthVO auth = new AuthVO();
			auth.setEntity(entity);
			
			auths.add(auth);
		}
		
		account.setAuths(auths);
		
		accountService.assignmentAuths(account);
		
		super.getRequest().setAttribute("result", "Success");
		
		return SUCCESS;
	}
	
	public String getAccountId() {
		return accountId;
	}
	
	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}
	
	public List<EntityVO> getEntitys() {
		return entitys;
	}
	
	public void setEntitys(List<EntityVO> entitys) {
		this.entitys = entitys;
	}
	
	public AccountVO getAccount() {
		return account;
	}
	
	public void setAccount(AccountVO account) {
		this.account = account;
	}

	public List<String> getEntityIds() {
		return entityIds;
	}

	public void setEntityIds(List<String> entityIds) {
		this.entityIds = entityIds;
	}
	
}
